﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UnDbxNET
{
    public partial class MainForm : Form
    {
        private FolderBrowserDialog m_FolderBrowser;
        private UpdateProgressDelegate UpdateProgress = null;
        private UpdateLogDelegate UpdateLog = null;

        delegate void UpdateProgressDelegate(int value);
        delegate void UpdateLogDelegate(string value);

        public MainForm()
        {
            InitializeComponent();
            m_FolderBrowser = new FolderBrowserDialog();
            UpdateProgress = new UpdateProgressDelegate(UpdateProgressSafe);
            UpdateLog = new UpdateLogDelegate(UpdateLogSafe);
            UpdateLogSafe("Program starts");
        }

        void UpdateProgressSafe(int value)
        {
            this.pbProgress.Value = value;
        }

        void UpdateLogSafe(string value)
        {
            tbLog.SelectedIndex = tbLog.Items.Add(String.Format(m_LogTpl, 
                DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToLongTimeString(), value));

            tbLog.ClearSelected();
        }

        private void bDbxDir_Click(object sender, EventArgs e)
        {
            DialogResult MyDialogResult = m_FolderBrowser.ShowDialog();
            if (MyDialogResult == DialogResult.OK)
            {
                tbDbxDir.Text = m_FolderBrowser.SelectedPath;
                if (tbOutDir.Text == "") tbOutDir.Text = m_FolderBrowser.SelectedPath;
            }
        }

        private void bEmlDir_Click(object sender, EventArgs e)
        {
            DialogResult MyDialogResult = m_FolderBrowser.ShowDialog();
            if (MyDialogResult == DialogResult.OK)
            {
                tbOutDir.Text = m_FolderBrowser.SelectedPath;
            }
        }

        private void bRun_Click(object sender, EventArgs e)
        {
            pbProgress.Value = 0;

            SortedList<string, object> arguments = new SortedList<string, object>();
            arguments.Add("DBX_DIR_IN", tbDbxDir.Text);
            arguments.Add("DBX_DIR_OUT", tbOutDir.Text);
            arguments.Add("DO_RECOVER", (Boolean)cbRecovery.Checked);

            UpdateLogSafe("Process starts");

            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.WorkerSupportsCancellation = true;
            worker.WorkerReportsProgress = false;
            worker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.RunWorkerAsync(arguments);

            bRun.Enabled = false;
            bDbxDir.Enabled = false;
            bEmlDir.Enabled = false;
            cbRecovery.Enabled = false;
            tbDbxDir.ReadOnly = true;
            tbOutDir.ReadOnly = true;
        }

        private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            SortedList<string, object> arguments = (SortedList<string, object>)e.Argument;
            undbx MyUnDbx = new undbx();
            MyUnDbx.ReportProgress += new ProgressEventHandler(undbx_ProgressChanged);
            MyUnDbx.ReportLogUpdate += new UpdateLogEventHandler(undbx_LogUpdate);
            MyUnDbx.unDbx((string)arguments["DBX_DIR_IN"], (string)arguments["DBX_DIR_OUT"], (bool)arguments["DO_RECOVER"]);
        }

        private void undbx_ProgressChanged(int e)
        {
            pbProgress.Invoke(UpdateProgress, new object[] { e });
        }

        private void undbx_LogUpdate(string e)
        {
            tbLog.Invoke(UpdateLog, new object[] { e });
        }

        private void worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            pbProgress.Value = 100;
            UpdateLogSafe("Process finished");
            bRun.Enabled = true;
            bDbxDir.Enabled = true;
            bEmlDir.Enabled = true;
            cbRecovery.Enabled = true;
            tbDbxDir.ReadOnly = false;
            tbOutDir.ReadOnly = false;
        }

        private string m_LogTpl = "[{0}] {1}...";
    }
}
